package net.caixiaomi.info.Lottery.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.sobot.chat.utils.ScreenUtils;

import net.caixiaomi.info.Lottery.callback.BallCallBack;
import net.caixiaomi.info.Lottery.model.BallItem;
import net.caixiaomi.info.R;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by suntian on 2018/8/2.
 */

public class BallView extends View {

    private int mLines;
    private int mRows;
    private int mTotals;
    private List<BallItem> ballList;
    private int mSpace;
    private int mMissSpace;
    private Context mContext;
    private int ballItemWidth;
    private int ballItemHeight;

//    private Bitmap bitmapNor;
//    private Bitmap bitmapPr;

//    private Paint mNumPaint;

    private Paint mNorPaint;
    private Paint mPrePaint;
    private Paint mNorTextPaint;
    private Paint mPreTextPaint;

    private Paint mMissPaint;

    private Paint mCirclePaint;

    private int mNumTextHeight;
    private int mMissTextHeight;

    private boolean mHasMiss=false;

    private int mCircleSize;

    private BallCallBack mCallBack;

    private int mType;  //0标准前区 1标准后区 2胆码红球 3拖码红球 4胆码蓝球 5拖码蓝球

    public BallView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.custom);
        if (a != null) {
            int count = a.getIndexCount();
            int index;
            for (int i = 0; i < count; i++) {
                index = a.getIndex(i);

                if (index == R.styleable.custom_rows) {
                    mRows = a.getInt(index, 0);
//                } else if (index == R.styleable.custom_lines) {
//                    mLines = a.getInt(index, 0);
                } else if (index == R.styleable.custom_counts) {
                    mTotals = a.getInt(index, 0);
                } else {

                }

            }
            a.recycle();

            ballList=new ArrayList<>();
            mLines=mTotals%mRows==0?mTotals/mRows:mTotals/mRows+1;

            int ballLine; //默认第一行
            for(int i=0;i<mTotals;i++){
                BallItem ball=new BallItem();
                ballLine=(i)/mRows+1;
                int ballRow=(mRows+i+1)%mRows==0?mRows:(mRows+i+1)%mRows;
                ball.setLine(ballLine);
                ball.setRow(ballRow);
                int num=(ballLine-1)*mRows+ballRow;
                ball.setNum(num);
                ball.setSelect(false);
//                ball.setMissNum("");
                ballList.add(ball);
            }

            //----
//            mNumPaint=new Paint();
//            mNumPaint.setColor(Color.WHITE);
//            mNumPaint.setTextSize(ScreenUtils.dip2px(mContext,15));
//            mMissPaint=new Paint();
//            mMissPaint.setColor(mContext.getResources().getColor(R.color.white_30));
//            mMissPaint.setTextSize(ScreenUtils.dip2px(mContext,14));
//
//            Rect rect = new Rect();
//            mNumPaint.getTextBounds("text", 0, "text".length(), rect);
//            mNumTextHeight = rect.height();//文本的高度
//
//            mSpace= ScreenUtils.dip2px(mContext,15);
//            mCircleSize=ScreenUtils.dip2px(mContext,1);
//
//            if(mHasMiss) {
//                mMissPaint.getTextBounds("text", 0, "text".length(), rect);
//                mMissTextHeight = rect.height();//文本的高度
//                mMissSpace=mMissTextHeight+ScreenUtils.dip2px(mContext,5);
////                mMissTextHeight=mMissTextHeight+ScreenUtils.dip2px(mContext,10);
//            }
        }
    }

    public void init(int norColor,int preColor,int norTextColor,int preTextColor,int type,BallCallBack callBack){

        mCallBack=callBack;
        mType=type;

        mNorPaint=new Paint();
        mNorPaint.setColor(norColor);
        mNorPaint.setTextSize(ScreenUtils.dip2px(mContext,15));
        mPrePaint=new Paint();
        mPrePaint.setColor(preColor);
        mPrePaint.setTextSize(ScreenUtils.dip2px(mContext,15));
        mNorTextPaint=new Paint();
        mNorTextPaint.setColor(norTextColor);
        mNorTextPaint.setTextSize(ScreenUtils.dip2px(mContext,15));
        mPreTextPaint=new Paint();
        mPreTextPaint.setColor(preTextColor);
        mPreTextPaint.setTextSize(ScreenUtils.dip2px(mContext,15));

        mMissPaint=new Paint();
        mMissPaint.setColor(mContext.getResources().getColor(R.color.white_30));
        mMissPaint.setTextSize(ScreenUtils.dip2px(mContext,14));

        mCirclePaint=new Paint();
        mCirclePaint.setColor(mContext.getResources().getColor(R.color.secondary_text));
        mCirclePaint.setTextSize(ScreenUtils.dip2px(mContext,14));

        Rect rect = new Rect();
        mNorTextPaint.getTextBounds("text", 0, "text".length(), rect);
        mNumTextHeight = rect.height();//文本的高度

        mSpace= ScreenUtils.dip2px(mContext,15);
        mCircleSize=ScreenUtils.dip2px(mContext,(float) 0.5);

//        if(mHasMiss) {
//            mMissPaint.getTextBounds("text", 0, "text".length(), rect);
//            mMissTextHeight = rect.height();//文本的高度
//            mMissSpace=mMissTextHeight+ScreenUtils.dip2px(mContext,5);
//        }
        mMissPaint.getTextBounds("text", 0, "text".length(), rect);
        mMissTextHeight = rect.height();//文本的高度
        mMissSpace=mMissTextHeight+ScreenUtils.dip2px(mContext,5);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width=getMeasuredWidth();

        width=width-(mRows+1)*mSpace;
        ballItemWidth=ballItemHeight=width/mRows;

//        bitmapNor=setImgSize(0,ballItemWidth,ballItemHeight);
//        bitmapPr=setImgSize(1,ballItemWidth,ballItemHeight);

        int height=ballItemWidth*mLines+mSpace*(mLines+1);

        if(mHasMiss){
            height=height+mLines*mMissSpace;
        }
//        if(mHasMiss) {
//            Rect rect = new Rect();
//            mMissPaint.getTextBounds("text", 0, "text".length(), rect);
//            mMissTextHeight = rect.height();//文本的高度
//            mMissSpace=mMissTextHeight+ScreenUtils.dip2px(mContext,5);
//        }

        setMeasuredDimension(getMeasuredWidth(), height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for(int i=0;i<ballList.size();i++){
            BallItem ball=ballList.get(i);
            int top;
            if(mHasMiss){
                top=(ball.getLine()-1)*(ballItemHeight+mSpace+mMissSpace)+mSpace;
            }else{
                top=(ball.getLine()-1)*(ballItemHeight+mSpace)+mSpace;
            }

            int left=(ball.getRow()-1)*ballItemWidth+mSpace*ball.getRow();

//            canvas.drawCircle(left+ballItemWidth/2, top+ballItemWidth/2, ballItemWidth/2+mCircleSize, mMissPaint);
//            canvas.drawCircle(left+ballItemWidth/2, top+ballItemWidth/2, ballItemWidth/2, mNumPaint);

            ball.setPositionX_Start(left);
            ball.setPositionX_Stop(left+ballItemWidth);

            ball.setPositionY_Start(top);
            ball.setPositionY_Stop(top+ballItemHeight);

            int ballTop=(ballItemHeight+mNumTextHeight)/2;

            String ballNum=ball.getNum()<10?("0"+ball.getNum()):(ball.getNum()+"");

            if(ball.isSelect()){
//                canvas.drawCircle(left+ballItemWidth/2, top+ballItemWidth/2, ballItemWidth/2+mCircleSize, mCirclePaint);
                canvas.drawCircle(left+ballItemWidth/2, top+ballItemWidth/2, ballItemWidth/2, mPrePaint);
                float numLength = mPreTextPaint.measureText(ballNum);
                float ballLeft=(ballItemWidth-numLength)/2;
                canvas.drawText(ballNum,left+ballLeft,top+ballTop,mPreTextPaint);
            }else{
                canvas.drawCircle(left+ballItemWidth/2, top+ballItemWidth/2, ballItemWidth/2+mCircleSize, mCirclePaint);
                canvas.drawCircle(left+ballItemWidth/2, top+ballItemWidth/2, ballItemWidth/2, mNorPaint);
                float numLength = mNorTextPaint.measureText(ballNum);
                float ballLeft=(ballItemWidth-numLength)/2;
                canvas.drawText(ballNum,left+ballLeft,top+ballTop,mNorTextPaint);
            }

            if(mHasMiss){
                String ballMiss=ball.getMissNum();
                if(ballMiss!=null) {
                    float missLength = mMissPaint.measureText(ballMiss);
                    float missLeft = (ballItemWidth - missLength) / 2;
                    int missTop = ballItemHeight + (mMissSpace + mSpace + mMissTextHeight) / 2;
                    canvas.drawText(ballMiss, left + missLeft, top + missTop, mMissPaint);
                }
            }
        }

    }

    private int mTouchIndex;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int x=(int) event.getX();
        int y=(int) event.getY();

        if(event.getAction()==MotionEvent.ACTION_DOWN){
            mTouchIndex=-1;
            for(int i=0;i<ballList.size();i++){
                BallItem ball=ballList.get(i);
                if(x>ball.getPositionX_Start()&&x<ball.getPositionX_Stop()&&y>ball.getPositionY_Start()&&y<ball.getPositionY_Stop()){
                    mTouchIndex=i;
                    break;
                }
            }
        }else if(event.getAction()==MotionEvent.ACTION_UP){
            for(int i=0;i<ballList.size();i++){
                BallItem ball=ballList.get(i);
                if(x>ball.getPositionX_Start()&&x<ball.getPositionX_Stop()&&y>ball.getPositionY_Start()&&y<ball.getPositionY_Stop()){
                    if(mTouchIndex==i){
                        //-------
                        boolean select=ball.isSelect();
                        ball.setSelect(!select);
                        //-------
                        if(mCallBack.gonChangeBallData(mType,ball)){
                            invalidate();
                        }else{
                            ball.setSelect(select);
                        }
                    }
                    break;
                }
            }
        }

        return true;
    }

    public List<BallItem> getBallData(){
        return ballList;
    }

    /**
     * 设置遗漏
     * @param list
     */
    public void setMissNum(List<BallItem> list){
        for(int i=0;i<list.size();i++){
            for(int j=0;j<ballList.size();j++){
                BallItem ballItem=ballList.get(j);
                BallItem item=list.get(i);
                if(ballItem.getNum()==item.getNum()){
                    ballItem.setMissNum(item.getMissNum());
                }
            }
        }
        invalidate();
    }

    /**
     * 设置遗漏开关
     */
    public void setMissSwitch(boolean hasMiss){
        mHasMiss=hasMiss;
        requestLayout();
    }

    /**
     * 设置选中球
     * @param list
     * @param clear
     */
    public void setCheckData(List<BallItem> list,boolean clear){
        if(clear) {
            clearAll();
        }
        if(list!=null) {
            for (int i = 0; i < list.size(); i++) {
                for (int j = 0; j < ballList.size(); j++) {
                    BallItem ballItem = ballList.get(j);
                    BallItem item = list.get(i);
                    if (ballItem.getNum() == item.getNum()) {
                        ballItem.setSelect(item.isSelect());
                    }
                }
            }
        }

        mCallBack.gonChangeBallData(mType,null);
        invalidate();
    }

    private void clearAll(){
        for(int i=0;i<ballList.size();i++){
            ballList.get(i).setSelect(false);
        }
    }

}
